package com.amos.teaching_aids.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.amos.teaching_aids.R;
import com.amos.teaching_aids.SysApplication;
import com.amos.teaching_aids.TitleLayout;
import com.amos.teaching_aids.presenter.IUserPresenter;
import com.amos.teaching_aids.presenter.UserPresenter;
import com.amos.teaching_aids.ui.Interface.IUserLoginView;

import static android.R.attr.button;
import static android.R.attr.defaultHeight;

/**
 * Created by Administrator on 2016/10/8 0008.
 */

public class UserLoginActivity extends BaseActivity implements IUserLoginView, View.OnClickListener
{
    private EditText userId, userPass;
    private RadioButton radioButton_s, radioButton_t;
    private IUserPresenter mUserPresenter;
    private Button register, confirm;

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUserPresenter = new UserPresenter(null, this, null);
        this.titleLayout.setTitle("登录");

    }

    @Override
    public int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        super.initView();
        userId = (EditText) findViewById(R.id.login_id);
        userPass = (EditText) findViewById(R.id.login_pass);
        radioButton_s = (RadioButton) findViewById(R.id.radio_s);
        radioButton_t = (RadioButton) findViewById(R.id.radio_t);
        register = (Button) findViewById(R.id.button_register);
        confirm = (Button) findViewById(R.id.button_confirm);
        register.setOnClickListener(this);
        confirm.setOnClickListener(this);
        this.progressDialog.setMessage("登录中...");
        this.progressDialog.setCancelable(false);
    }

    public void login(){
        if(!(radioButton_s.isChecked() || radioButton_t.isChecked())){
            Toast.makeText(this, "请选择您的身份", Toast.LENGTH_SHORT).show();
        }
        else {
            mUserPresenter.saveRadioCheck(radioButton_t.isChecked(), radioButton_s.isChecked());
            String identify = radioButton_s.isChecked() ? "student" : "teacher";
            mUserPresenter.login(userId.getText().toString(), userPass.getText().toString(),identify );
        }

    }

    public void showRegActivity(){
        if(!(radioButton_s.isChecked() || radioButton_t.isChecked())){
            Toast.makeText(this, "请选择您的身份", Toast.LENGTH_SHORT).show();
        }
        else {
            mUserPresenter.saveRadioCheck(radioButton_t.isChecked(), radioButton_s.isChecked());
            Intent intent = new Intent(UserLoginActivity.this, UserRegisterActivity.class);
            startActivity(intent);
        }

    }
    public String getIdentify(){
        String identify;
        if (radioButton_s.isChecked()){
            identify = "student";
        }
        else {
            identify = "teacher";
        }
        return identify;
    }

    @Override
    public void showLoding() {
        this.progressDialog.show();

    }

    @Override
    public void hideLoding() {
        this.progressDialog.dismiss();

    }

    @Override
    public void showLoginSuc() {
        Toast.makeText(SysApplication.getContext(), "登录成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoginFail(String errorReason) {
        Toast.makeText(SysApplication.getContext(), "登录失败," + errorReason, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button_register:
                showRegActivity();
                break;
            case R.id.button_confirm:
                login();
                break;
            default:
                break;
        }
    }
}
